#include "GameTexture.h"

GameTexture::GameTexture()
{
	m_core = 0;
	m_bHover = false;
	m_bHover = false;
	m_rot = 0.0f;

	m_animated = false;
	m_aniTick = 0.0f;
	m_aniTime = 0.05f;
	m_aniCellHor = 0;
	m_aniCellVer = 0;
	m_aniNum = 0;
	m_aniRow = 0;
	m_aniCol = 0;
}

GameTexture::~GameTexture()
{

}

void GameTexture::initTexture(BeanieCore* a_core, char* a_filePath,
	int a_cen, V2DF a_pos, V2DF a_scl, float a_rot, bool a_camLock)
{
	m_core = a_core;
	m_core->createTexture(&m_texture,a_filePath);

	float fw = (m_texture.m_width*a_scl.x);
	float fh = (m_texture.m_height*a_scl.y);
	float hw = fw/2.0f;
	float hh = fh/2.0f;

	m_min.set(0,0);
	m_max.set(fw,fh);

	m_texture.m_texMin.set(m_min);
	m_texture.m_texMax.set(m_max/a_scl);
	m_texture.m_camLock = a_camLock;

	switch(a_cen)
	{
	case TEX_TOP:
		m_texture.m_texCen.set(hw,0);
		break;
	case TEX_BOT:
		m_texture.m_texCen.set(hw,fh);
		break;
	case TEX_LEFT:
		m_texture.m_texCen.set(0,hh);
		break;
	case TEX_RIGHT:
		m_texture.m_texCen.set(fw,hh);
		break;
	case TEX_TOPLEFT:
		m_texture.m_texCen.set(0,0);
		break;
	case TEX_TOPRIGHT:
		m_texture.m_texCen.set(fw,0);
		break;
	case TEX_BOTLEFT:
		m_texture.m_texCen.set(0,fh);
		break;
	case TEX_BOTRIGHT:
		m_texture.m_texCen.set(fw,fh);
		break;
	case TEX_CENTER:
		m_texture.m_texCen.set(hw,hh);
		break;
	};

	m_pos = a_pos;
	m_scl = a_scl;
	m_rot = a_rot;
	m_min += m_pos + m_texture.m_texCen;
	m_max += m_pos + m_texture.m_texCen;
}

void GameTexture::initTexture(BeanieCore* a_core, char* a_filePath,
	int a_cen, V2DF a_pos, V2DF a_scl, float a_rot, bool a_camLock,
	int a_cellHor, int a_cellVer)
{
	m_core = a_core;
	m_core->createTexture(&m_texture,a_filePath);

	m_animated = true;
	m_aniCellHor = a_cellHor;
	m_aniCellVer = a_cellVer;

	float cfw = ((float)m_texture.m_width/a_cellHor);
	float cfh = ((float)m_texture.m_height/a_cellVer);
	float chw = cfw/2.0f;
	float chh = cfh/2.0f;

	m_min.set(0,0);
	m_max.set(cfw,cfh);

	m_texture.m_texMin.set(m_min);
	m_texture.m_texMax.set(m_max);
	m_texture.m_camLock = a_camLock;

	switch(a_cen)
	{
	case TEX_TOP:
		m_texture.m_texCen.set(chw,0);
		break;
	case TEX_BOT:
		m_texture.m_texCen.set(chw,cfh);
		break;
	case TEX_LEFT:
		m_texture.m_texCen.set(0,chh);
		break;
	case TEX_RIGHT:
		m_texture.m_texCen.set(cfw,chh);
		break;
	case TEX_TOPLEFT:
		m_texture.m_texCen.set(0,0);
		break;
	case TEX_TOPRIGHT:
		m_texture.m_texCen.set(cfw,0);
		break;
	case TEX_BOTLEFT:
		m_texture.m_texCen.set(0,cfh);
		break;
	case TEX_BOTRIGHT:
		m_texture.m_texCen.set(cfw,cfh);
		break;
	case TEX_CENTER:
		m_texture.m_texCen.set(chw,chh);
		break;
	};

	m_pos = a_pos;
	m_scl = a_scl;
	m_rot = a_rot;
	m_min += m_pos + m_texture.m_texCen;
	m_max += m_pos + m_texture.m_texCen;
}

void GameTexture::input()
{
	int x, y;
	m_core->mousePosition(&x,&y);

	V2DF mPos((float)x,(float)y);
	if(pointInAABB(mPos,m_min,m_max))
	{
		m_bHover = true;
		if(m_core->mousePress(BTN_LEFT))
			m_bClick = true;
		else
			m_bClick = false;
	}
	else
	{
		m_bHover = false;
		m_bClick = false;
	}
}

void GameTexture::update()
{
	m_core->updateTransform(&m_texture,
		m_pos,m_scl,m_rot,
		&m_pos,&m_min,&m_max);
}

void GameTexture::draw()
{
	m_core->drawTexture(&m_texture);
}

void GameTexture::aniPlay()
{
	if(m_animated)
	{
		m_aniTick += m_core->getElapsed();

		if(m_aniTick > m_aniTime)
		{
			float hmove = ((float)m_texture.m_width)/m_aniCellHor;
			if(m_aniCol == m_aniCellHor-1)
			{
				m_aniCol = 0;
				m_texture.m_texMin.x = 0;
				m_texture.m_texMax.x = hmove;

				float vmove = ((float)m_texture.m_height)/m_aniCellVer;
				if(m_aniRow == m_aniCellVer-1)
				{
					m_aniRow = 0;
					m_texture.m_texMin.y = 0;
					m_texture.m_texMax.y = vmove;
				}
				else
				{
					m_texture.m_texMin.y += vmove;
					m_texture.m_texMax.y += vmove;
					m_aniRow++;
				}
			}
			else
			{
				m_texture.m_texMin.x += hmove;
				m_texture.m_texMax.x += hmove;
				m_aniCol++;
			}

			m_aniNum++;
			m_aniTick = 0.0f;
		}
	}
}